<?php
/**
 * Created by PhpStorm.
 * User: glq
 * Date: 2018/11/5
 * Time: 16:12
 */

namespace Xiaoqu\Controller;

use Common\Controller\AdminbaseController;
use Label\Model\CategoryModel;

class AdminxiaoquController extends AdminbaseController {
    protected $page_size;
    function _initialize() {
        parent::_initialize();
        $this->page_size    = 20;
    }

    // 小区列表
    public function index() {
        $areas = getArea(3);
        $this->assign('area', json_encode($areas));
        $this->_lists($areas);
        $this->display();
    }

    // 编辑
    public function edit_xiaoqu()
    {
        $id     = I('get.id');
        $model  = M('xiaoqu');
        $info   = $model
            ->where(['id' => $id])
            ->field('*, (select group_concat(name) from rx_category where find_in_set(id, rx_xiaoqu.categorys)) as category_names, (select areaname from rx_area where rx_xiaoqu.city_id = id) as city_name')
            ->find();
        if ($info) {
            $del = I('del');
            if ($del) {
                $model->where(['id' => $id])->delete();
                $this->success('删除成功');exit;
            }

            if (IS_POST) {
                // 编辑
                $_POST['info']['categorys'] = implode(',', $_POST['cate']);
                $info = I('post.info');
                $model->where(['id' => $id])->save($info);
                $this->success('编辑成功');
            } else {
                //项目分类
                $categorys = M('category')->where(['type' => CategoryModel::CATEGORY_TYPE_XIAOQU, 'status' => 1])->select();
                $this->assign('categorys', $categorys);

                $info['categorys'] = array_filter(explode(',', $info['categorys']));
                $this->assign('info', $info);
                $this->display();
            }
        } else {
            $this->error('数据有误');
        }
    }

    /**
     * @param array $where 查询条件
     */
    public function _lists($areas)
    {
        $where = array();
        // 分类
        $category_id = I('request.category_id');
        if(!empty($category_id)){
            $where[] = "FIND_IN_SET(" . $category_id . ", categorys)";
        }
        //项目分类
        $categorys = M('category')->where(['type' => CategoryModel::CATEGORY_TYPE_XIAOQU, 'status' => 1])->select();
        $this->assign('categorys', $categorys);

        // 名称
        $name = I('request.name');
        if(!empty($name)){
            $where['name'] = array('like',"%$name%");
        }

        // 城市
        $city_id = I('request.city_id');
        if(!empty($city_id)){
            $all_city_id = getAreaParentPath($city_id, $areas);
            $where[] = "FIND_IN_SET(city_id, '" . implode(',', $all_city_id) . "')";

            $this->assign('path', json_encode($all_city_id));
        }

        // 年份
        $year_id    = I('request.year');
        $year       = array('', 'build_time <= 1995', 'build_time > 1995 and build_time <= 2000', 'build_time > 2000 and build_time <= 2005', 'build_time > 2005 and build_time <= 2010', 'build_time > 2010');
        if(!empty($year_id) && isset($year[$year_id])){
            $where[] = $year[$year_id];
        }

        $model  = M('xiaoqu');
        $count  = $model->where($where)->count();
        $page   = $this->page($count, $this->page_size);
        $lists  = $model
            ->where($where)
            ->order('build_time desc')
            ->limit($page->firstRow , $page->listRows)
            ->field('*, (select group_concat(name) from rx_category where find_in_set(id, rx_xiaoqu.categorys)) as category_names, (select areaname from rx_area where rx_xiaoqu.city_id = id) as city_name')
            ->select();
//        foreach ($lists as $k => $v) {
//            $v['category_names'] = array_filter(explode(',', $v['category_names']));
//            $lists[$k] = $v;
//        }
        $this->assign("page", $page->show('admin'));
        $this->assign("formget", array_merge($_GET,$_POST));
        $this->assign("lists", $lists);
        $this->assign('total_pages', ceil($count / $this->page_size));
    }
}